package com.example.leon.demo.Activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.leon.demo.R;

import com.example.leon.demo.RewriteParentControl.ReToast;
import com.example.leon.demo.adapter.MsgAdapter;
import com.example.leon.demo.common.BaseActivity;
import com.example.leon.demo.entity.Msg;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by wang.long on 2016/3/29.
 */
public class ChatActivity extends BaseActivity {

    private ListView msgListView;

    private EditText inputText;

    private Button send;

    private MsgAdapter msgAdapter;

    private List<Msg> msgList=new ArrayList<Msg>();


    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

        super.setContentView(R.layout.chat_activity);
        initMsgs(); // 初始化消息数据

        msgAdapter=new MsgAdapter(ChatActivity.this,R.layout.msg_item,msgList);

        inputText=(EditText) findViewById(R.id.input_text);

        send=(Button) findViewById(R.id.send);

        msgListView=(ListView) findViewById(R.id.msg_list_view);

        msgListView.setAdapter(msgAdapter);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String content=inputText.getText().toString();

                if(!"".equals(content)){
                    Msg msg=new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);

                    msgAdapter.notifyDataSetChanged();

                    msgListView.setSelection(msgList.size());

                    inputText.setText("");

                } else {
                    ReToast.showToast("请输入消息",ChatActivity.this);
                }
            }
        });

    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }


}
